<?php
    /**
     * Created by PhpStorm.
     * User: dingjinlai
     * Date: 2019/5/24
     * Time: 上午10:21
     */

    defined('BASEPATH') OR exit('No direct script access allowed');

    /**
     * @property ali          ali
     * @property  sys_message sys_message
     */
    class PrepareGoods extends Admin_Controller
    {

        /**
         * 产品池列表
         * 待审核列表
         */
        public function index_accurate()
        {
            $this->load->library('pagination');
            $this->load->model('prepare_goods_model');
            $assoc = $this->uri->uri_to_assoc();
            $this->load->model('category_model');
            $this->config->load('website_common');
            $production = $this->config->item('production');
            $reason     = $this->config->item('reason');
            $this->load->model('admin_model');

            $_param_option = array(
                'admin_id'   => array(
                    'col'         => 'col-lg-2',
                    'type'        => 'option',
                    'placeholder' => '管理员',
                    'option'      => $this->admin_model->admin_option(),
                ),
                'goods_sn'   => array(
                    'col'         => 'col-lg-3',
                    'type'        => 'text',
                    'placeholder' => '货号',
                ),
                'cate_name'  => array(
                    'col'         => 'col-lg-2',
                    'type'        => 'option_cat',
                    'placeholder' => '选择分类',
                    'option'      => $this->category_model->cat_one(),
                ),
                'production' => array(
                    'col'         => 'col-lg-2',
                    'type'        => 'option',
                    'placeholder' => '生产方式',
                    'option'      => $production,
                ),
            );

            $_param        = array(
                'admin_id'      => intval($this->input->get('admin_id')),
                'goods_sn'      => $this->input->get('goods_sn'),
                'cate_name'     => intval($this->input->get('cate_name')),
                'production'    => intval($this->input->get('production')),
                'page'          => $assoc['page'] > 0 ? intval($assoc['page']) - 1 : 0,
                'prepare_state' => intval($this->input->get('prepare_state')),
            );
            $prepare_state = $this->input->get('prepare_state');
            //分页
            $count                      = $this->prepare_goods_model->prepare_num($_param);
            $config['base_url']         = base_url('PrepareGoods/') . 'index_accurate/page/';
            $config['total_rows']       = $count;
            $config['num_links']        = 6;
            $config['per_page']         = $this->prepare_goods_model->limit_rows;
            $config['use_page_numbers'] = true;
            $this->pagination->initialize($config);

            $prepare_accurate_list = $this->prepare_goods_model->prepare_accurate_list($_param);
            $data                  = array(
                'pages'                 => $this->pagination->create_links(),
                'prepare_accurate_list' => $prepare_accurate_list,
                'param_option'          => $_param_option,
                'param'                 => $_param,
                'reason'                => $reason,
                'count'                 => $count,
            );
            if ($prepare_state == 1){
                $this->load->view('access_table/prepare_accurate_list', $data);
            }
            if ($prepare_state == 2){
                $this->load->view('access_table/index_supplement', $data);
            }
            if ($prepare_state == 3){
                $this->load->view('access_table/index_edited', $data);
            }
            if ($prepare_state == 4){
                $this->load->view('access_table/index_uppershelf', $data);
            }
            if ($prepare_state == 5){
                $this->load->view('access_table/index_complete', $data);
            }
            if ($prepare_state == 6){
                $this->load->view('access_table/index_recovery', $data);
            }

        }

        //批量审核
        public function all_examine()
        {
            $this->load->model('prepare_goods_model');
            $all_id     = $this->input->post('all_id');
            $all_id_arr = explode(',', substr($all_id, 1));
            $data       = array(
                'goods_id'    => $all_id_arr,
                'is_adopt'    => $this->input->post('is_adopt'),
                'reason_text' => $this->input->post('reason_text'),
            );

            $this->load->model('prepare_goods_model');
            $res = $this->prepare_goods_model->examine_goods($data);
            echo $res;
            die;
        }

        //商品加色展示 废弃
        public function index_color()
        {
            return;
            $this->load->model('prepare_goods_model');
            $prepare_id = intval($this->input->get('prepare_id'));
            $post_sku   = $this->prepare_goods_model->select_find('goods_supp', ['prepare_id' => $prepare_id], 'post_sku');
            $post_sku   = json_decode($post_sku['post_sku'], true);
            $color      = $this->prepare_goods_model->prepare_table('colors', ['is_delete' => 0], '*');
            $size       = $this->prepare_goods_model->prepare_table('size', ['is_delete' => 0], '*');
            $data       = array(
                'post_sku'   => $post_sku,
                'color'      => $color,
                'size'       => $size,
                'prepare_id' => $prepare_id,
            );

            $this->load->view('access_table/index_color', $data);
        }

        //商品加色添加 废弃
        public function index_add_color()
        {
            return;
            $this->load->model('prepare_goods_model');
            $this->load->library('sys_message');
            $assoc      = $this->uri->uri_to_assoc();
            $prepare_id = intval($assoc['prepare_id']);

            $aliyun_sku = $this->input->post('aliyun_sku');

            $ali_sku = [];
            foreach ($aliyun_sku['ali_color'] as $key => $value) {
                $ali_sku['color'][$key]['ali_value'] = $value;
                $ali_sku['color'][$key]['value']     = $aliyun_sku['color'][$key];
            }

            foreach ($aliyun_sku['ali_size'] as $key => $value) {
                $ali_sku['size'][$key]['ali_value'] = $value;
                $ali_sku['size'][$key]['value']     = $aliyun_sku['size'][$key];
            }

            !array_filter($aliyun_sku['color']) && $this->sys_message->wrong('请选择颜色!');
            !array_filter($aliyun_sku['size']) && $this->sys_message->wrong('请选择尺码!');

            //修改goods_supp
            $this->prepare_goods_model->supp_update($prepare_id, $ali_sku);
            //查询goods_json找ali SKU id
            $goods_json = $this->prepare_goods_model->select_find('goods_supp', ['prepare_id' => $prepare_id], 'goods_json');
            $goods_arr  = json_decode($goods_json['goods_json'], true);
            //print_R($goods_arr['skuMap']);
            //print_r($aliyun_sku);die;
            foreach ($aliyun_sku['ali_color'] as $color_k => $color_v) {
                foreach ($aliyun_sku['ali_size'] as $size_k => $size_v) {
                    if ($aliyun_sku['color'][$color_k] != ''){
                        if ($aliyun_sku['size'][$size_k] != ''){
                            $arr[] = [
                                'specid' => $goods_arr['skuMap'][$color_v . '&gt;' . $size_v]['specId'],
                                'color'  => $aliyun_sku['color'][$color_k],
                                'size'   => $aliyun_sku['size'][$size_k],
                            ];
                        }
                    }
                }
            }
            $this->prepare_goods_model->index_add_color($prepare_id, $arr);
            $this->sys_message->success('成功!');
        }

        //审核通过不通过
        public function examine_goods()
        {
            $this->load->library('sys_message');
            $data = array(
                'goods_id'    => $this->input->post('goods_id'),
                'is_adopt'    => $this->input->post('is_adopt'),
                'reason_text' => $this->input->post('reason_text'),
            );
            $this->load->model('prepare_goods_model');
            $res = $this->prepare_goods_model->examine_goods($data);
            $this->sys_message->success('成功!');
        }


        //操作日志查询
        public function select_log()
        {
            $this->load->model('prepare_goods_model');
            $id     = intval($this->input->post('id'));
            $return = $this->prepare_goods_model->select_log($id);
            echo json_encode($return);
        }

        //回收
        public function recovery()
        {
            $this->load->model('prepare_goods_model');
            $data = array(
                'goods_id'      => $this->input->post('goods_id'),
                'reason_reason' => $this->input->post('reason_reason'),
            );
            $res  = $this->prepare_goods_model->recovery($data);
            echo $res;
            die;
        }

        //还原
        public function reduction()
        {
            $this->load->model('prepare_goods_model');
            $data = array(
                'goods_id'    => $this->input->post('goods_id'),
                'reason_text' => $this->input->post('reason_text'),
            );
            $res  = $this->prepare_goods_model->reduction($data);
            echo $res;
            die;
        }

        /**
         * 产品池流程添加
         *
         *
         */

        public function set()
        {

            $this->load->model('admin_model');
            $this->load->model('category_model');
            $this->load->model('prepare_goods_model');
            $this->load->model('suppliers_model');
            $this->load->model('size_model');
            $this->load->model('color_model');
            $this->load->model('goods_type_model');


            $this->config->load('website_common');

            $this->load->library('sys_message');

            $assoc = $this->uri->uri_to_assoc();

            $prepare_id = intval($assoc['prepare_id']);
            $step       = intval($assoc['step']) ? intval($assoc['step']) : 1;

            $admin_name  = $this->admin_model->admin_option(['group' => 1]);
            $cate_option = $this->category_model->cate_option();
            $size        = $this->size_model->size_option();
            $colors      = $this->color_model->color_option();

            $prepare = array();
            if ($prepare_id){
                $prepare               = $this->prepare_goods_model->info($prepare_id);
                $prepare['goods_attr'] = json_decode($prepare['goods_attr'], true);
            }

            $config = $this->config->item('upload');
            $this->load->library('upload', $config);

            switch ($step) {
                case 1: // 开发上传产品
                    if ($_POST){
                        if ($this->input->post('production_id') == 2){
                            $admin_id   = intval($_SESSION['admin_id']) ? intval($_SESSION['admin_id']) : intval($this->input->post('admin_id'));
                            $aliyun_sku = $this->input->post('aliyun_sku');
                            !array_filter($aliyun_sku['color']) && $this->sys_message->wrong('请选择颜色!');
                            !array_filter($aliyun_sku['size']) && $this->sys_message->wrong('请选择尺码!');
                            $ali_sku = array();
                            foreach ($aliyun_sku['ali_color'] as $al_c => $ali_color) {
                                $ali_sku['color'][$al_c] = array(
                                    'ali_value' => $ali_color,
                                    'value'     => $aliyun_sku['color'][$al_c],
                                );
                            }

                            foreach ($aliyun_sku['ali_size'] as $al_s => $ali_size) {
                                $ali_sku['size'][$al_s] = array(
                                    'ali_value' => $ali_size,
                                    'value'     => $aliyun_sku['size'][$al_s],
                                );
                            }
                            $type_style = implode(',',$this->input->post('type_style'));
                            $form_data = array(
                                'purchase_id'   => intval($this->input->post('purchase_id')),
                                'admin_id'      => $admin_id,
                                'goods_sn'      => $this->prepare_goods_model->goods_sn_max($admin_id),
                                'cate_id'       => intval($this->input->post('cate_id')),
                                'fabric_id'     => intval($this->input->post('fabric_id')),
                                'channel_id'    => intval($this->input->post('channel_id')),
                                'production_id' => intval($this->input->post('production_id')),
                                'sale_status'   => (int)($this->input->post('sale_status')),
                                'ali_sku'       => $ali_sku,
                                'supp_url'      => trim(strval($this->input->post('supp_url'))),
                                'in_price'      => floatval(trim($this->input->post('in_price'))),
                                'shop_price'    => floatval(trim($this->input->post('shop_price'))),
                                'type_style'    => $type_style,
                            );
                            !$form_data['type_style'] && $this->sys_message->wrong('请选择风格');
                            // !$form_data['purchase_id'] && $this->sys_message->wrong('请选择采购员');
                            !$form_data['fabric_id'] && $this->sys_message->wrong('请选择面料');
                            !$form_data['cate_id'] && $this->sys_message->wrong('请选择分类');
                            !$form_data['production_id'] && $this->sys_message->wrong('请选择生产方式');
                            !$form_data['supp_url'] && $this->sys_message->wrong('1688链接');
                            !$form_data['in_price'] && $this->sys_message->wrong('请填写进货价!');
                            !$form_data['shop_price'] && $this->sys_message->wrong('请填写进价!');
                            !$form_data['sale_status'] && $this->sys_message->wrong('请选择销售状态!');
                            if ($this->upload->do_upload('file')){
                                $data_images = $this->upload->data();

                                $form_data['images'] = $data_images;
                            } else {
                                $this->sys_message->wrong('请上传图片!');
                            }
                            $this->prepare_goods_model->step_1_add($form_data);
                            $this->sys_message->success('成功!');
                        } else {
                            $admin_id   = intval($_SESSION['admin_id']) ? intval($_SESSION['admin_id']) : intval($this->input->post('admin_id'));
                            $aliyun_sku = $this->input->post('aliyun_sku');
                            if ($aliyun_sku['color'] && $aliyun_sku['size']){
                                !array_filter($aliyun_sku['color']) && $this->sys_message->wrong('请选择颜色!');
                                !array_filter($aliyun_sku['size']) && $this->sys_message->wrong('请选择尺码!');
                            } else {
                                $this->sys_message->wrong('颜色和尺码必须存在!');
                            }

                            $ali_sku = array();
                            foreach ($aliyun_sku['ali_color'] as $al_c => $ali_color) {
                                $ali_sku['color'][$al_c] = array(
                                    'ali_value' => $ali_color,
                                    'value'     => $aliyun_sku['color'][$al_c],
                                );
                            }

                            foreach ($aliyun_sku['ali_size'] as $al_s => $ali_size) {
                                $ali_sku['size'][$al_s] = array(
                                    'ali_value' => $ali_size,
                                    'value'     => $aliyun_sku['size'][$al_s],
                                );
                            }
                            $type_style = implode(',',$this->input->post('type_style'));
                            $form_data = array(
                                'purchase_id'   => intval($this->input->post('purchase_id')),
                                'admin_id'      => $admin_id,
                                'goods_sn'      => $this->prepare_goods_model->goods_sn_max($admin_id),
                                'cate_id'       => intval($this->input->post('cate_id')),
                                'fabric_id'     => intval($this->input->post('fabric_id')),
                                'channel_id'    => intval($this->input->post('channel_id')),
                                'production_id' => intval($this->input->post('production_id')),
                                'sale_status'   => (int)($this->input->post('sale_status')),
                                'supp_name'  => trim(strval($this->input->post('supp_name'))),
                                'supp_url'   => trim(strval($this->input->post('supp_url'))),
                                'supp_sn'    => trim(strval($this->input->post('supp_sn'))),
                                'ali_sku'    => $ali_sku,
                                'in_price'   => floatval(trim($this->input->post('in_price'))),
                                'shop_price' => floatval(trim($this->input->post('shop_price'))),
                                'g_supp_id'  => intval($this->input->post('g_supp_id')),
                                'type_style'    => $type_style,
                            );
                            !$form_data['type_style'] && $this->sys_message->wrong('请选择风格');
                            //!$form_data['purchase_id'] && $this->sys_message->wrong('请选择采购员');
                            !$form_data['cate_id'] && $this->sys_message->wrong('请选择分类');
                            !$form_data['fabric_id'] && $this->sys_message->wrong('请选择面料');
                            !$form_data['production_id'] && $this->sys_message->wrong('请选择生产方式');
                            !$form_data['supp_url'] && $this->sys_message->wrong('请填写链接!');
                            !$form_data['shop_price'] && $this->sys_message->wrong('请填写进价!');

                            if ($this->upload->do_upload('file')){
                                $data_images = $this->upload->data();

                                $form_data['images'] = $data_images;
                            } else {
                                $this->sys_message->wrong('请上传图片!');
                            }

                            $this->prepare_goods_model->step_1_add($form_data);
                            // $this->prepare_goods_model->prepare_update($form_data, 3, $prepare_id);
                            $this->sys_message->success('成功!');
                        }

                    }

                    break;
                case 2: // 开发补充产品
                    !$prepare && $this->sys_message->wrong('产品不存在!');
                    $prepare['prepare_state'] != 2 && $this->sys_message->wrong('产品不是待补充状态!');

                    if ($prepare['goods_attr']){
                        $prepare['goods_attr'] = json_decode($prepare['goods_attr'], true);
                    }

                    if ($_POST){

                        $aliyun_sku = $this->input->post('aliyun_sku');

                        !array_filter($aliyun_sku['color']) && $this->sys_message->wrong('请选择颜色!');
                        !array_filter($aliyun_sku['size']) && $this->sys_message->wrong('请选择尺码!');

                        $ali_sku = array();
                        foreach ($aliyun_sku['ali_color'] as $al_c => $ali_color) {
                            $ali_sku['color'][$al_c] = array(
                                'ali_value' => $ali_color,
                                'value'     => $aliyun_sku['color'][$al_c],
                            );
                        }

                        foreach ($aliyun_sku['ali_size'] as $al_s => $ali_size) {
                            $ali_sku['size'][$al_s] = array(
                                'ali_value' => $ali_size,
                                'value'     => $aliyun_sku['size'][$al_s],
                            );
                        }

                        $form_data = array(
                            'supp_name'  => trim(strval($this->input->post('supp_name'))),
                            'supp_url'   => trim(strval($this->input->post('supp_url'))),
                            'supp_sn'    => trim(strval($this->input->post('supp_sn'))),
                            'ali_sku'    => $ali_sku,
                            'in_price'   => floatval(trim($this->input->post('in_price'))),
                            'shop_price' => floatval(trim($this->input->post('shop_price'))),
                            'g_supp_id'  => intval($this->input->post('g_supp_id')),
                        );

                        !$form_data['supp_url'] && $this->sys_message->wrong('请填写链接!');
                        !$form_data['shop_price'] && $this->sys_message->wrong('请填写进价!');

                        $this->prepare_goods_model->prepare_update($form_data, 3, $prepare_id);

                        $this->sys_message->success('成功!');
                    }

                    break;
                case 3: // 编辑完善产品
                    $prepare['prepare_state'] <= 2 && $this->sys_message->wrong('产品信息不全!');
                    $type_option      = $this->goods_type_model->type_option();
                    $suppliers_option = $this->suppliers_model->suppliers_options();
                    $goods_supp       = $this->prepare_goods_model->goods_supp_info($prepare_id);
                    $goods_album      = $this->prepare_goods_model->goods_album($prepare_id);
                    $goods_option     = $this->prepare_goods_model->goods_option($prepare_id);
                    $prepare_sku      = $this->prepare_goods_model->goods_sku($prepare_id);

                    // print_r($goods_supp);
                    //print_r($goods_option);

                    $_goods_option = array();
                    foreach ($goods_option as $option) {
                        $_goods_option[$option['type_id']][$option['option_value']] = $option;
                    }

                    if (count($_goods_option[1]) > count($_goods_option[2])){
                        $for_option   = $_goods_option[1];
                        $for_2_option = $_goods_option[2];
                    } else {
                        $for_option   = $_goods_option[2];
                        $for_2_option = $_goods_option[1];
                    }


                    $sku = array();
                    foreach ($for_option as $k => $item) {
                        foreach ($for_2_option as $_k => $row) {
                            $sku[] = array(
                                'color' => $_goods_option[1][$_k]['option_value'],
                                'size'  => $_goods_option[2][$k]['option_value'],
                            );
                        }
                    }


                    if ($_POST){

                        $attr      = array(
                            'attr_title' => $this->input->post('attr_title'),
                            'attr_val'   => $this->input->post('attr_val'),
                        );
                        $form_data = array(
                            'title'        => trim($this->input->post('goods_name')),
                            'cn_name'      => trim($this->input->post('cn_name')),
                            'type_id'      => intval($this->input->post('type_id')),
                            'goods_weight' => floatval(trim($this->input->post('goods_weight'))),
                            'shop_price'   => floatval(trim($this->input->post('shop_price'))),
                            'body_html'    => trim($this->input->post('summernote')),
                            'suppliers_id' => intval($this->input->post('suppliers_id')),
                            'g_supp_id'    => $goods_supp['g_supp_id'],
                            'goods_attr'   => $attr,
                            'is_testing'   => intval($this->input->post('is_testing')),
                            'tags'         => $this->input->post('tags'),
                            'purchase_id'         => $this->input->post('purchase_id'),
                        );
                        !$form_data['purchase_id'] && $this->sys_message->wrong('选择采购员!');
                        !$form_data['title'] && $this->sys_message->wrong('请填写英文名称!');
                        !$form_data['cn_name'] && $this->sys_message->wrong('请填写中文名称!');
                        !$form_data['suppliers_id'] && $this->sys_message->wrong('请选择供应商!');
                        !$form_data['type_id'] && $this->sys_message->wrong('请选择类型!');
                        !$form_data['goods_weight'] && $this->sys_message->wrong('请填写重量!');
                        !$form_data['shop_price'] && $this->sys_message->wrong('请填写价格!');
                        if ($form_data['goods_weight'] < 20){
                            $this->sys_message->wrong('重量要大于30克!');
                        }
                        //img
                        $files_sort = $this->input->post('files_sort');
                        !$files_sort && $this->sys_message->wrong('请上传图片!');

                        foreach ($files_sort as $k => $val) {

                            if ($val){
                                $form_data['images'][$k] = array(
                                    'file_name' => $goods_album[$val]['original_img'],
                                    'handle'    => 'update',
                                    'album_id'  => $val,
                                );
                            } else {
                                if ($this->upload->do_upload('files' . $k)){
                                    $data_images             = $this->upload->data();
                                    $data_images['handle']   = 'insert';
                                    $form_data['images'][$k] = $data_images;
                                }
                            }
                        }


                        $form_data['original_img'] = reset($form_data['images']);

                        if ($prepare){
                            foreach ($goods_album as $k => $image) {
                                $album_id = $image['album_id'];
                                $r        = array_filter($form_data['images'], function($t) use ($album_id) {
                                    return $t['album_id'] == $album_id;
                                });
                                if ( !$r){
                                    $form_data['images'][] = array(
                                        'handle'   => 'delete',
                                        'album_id' => $image['album_id'],
                                    );
                                }

                            }
                        }

                        $aliyun_sku = $this->input->post('aliyun_sku'); //尺寸信息
                        !$aliyun_sku && $this->sys_message->wrong('请选择sku信息!');

                        //option
                        if ($aliyun_sku['color_img']){
                            foreach ($aliyun_sku['color_img'] as $k => $color_img) {
                                $form_data['option'][$k]['album_url'] = $form_data['images'][$color_img]['file_name'];
                            }
                        }


                        //sku
                        $sku = array();
                        if (count($aliyun_sku['ali_color']) > count($aliyun_sku['ali_size'])){
                            foreach ($aliyun_sku['ali_color'] as $k_1 => $color_val) {
                                foreach ($aliyun_sku['ali_size'] as $k_2 => $size_val) {

                                    $sku[] = array(
                                        'specid' => $goods_supp['goods_json']['skuMap']["{$goods_option[$k_1]['ali_value']}&gt;{$goods_option[$k_2]['ali_value']}"]['specId'],
                                        'color'  => $color_val,
                                        'size'   => $size_val,
                                        //'img' => $form_data['images'][$aliyun_sku['color_img'][$k_2]]['file_name']
                                    );
                                }
                            }
                        } else {
                            foreach ($aliyun_sku['ali_size'] as $k_1 => $size_val) {
                                foreach ($aliyun_sku['ali_color'] as $k_2 => $color_val) {

                                    $sku[] = array(
                                        'specid' => $goods_supp['goods_json']['skuMap']["{$goods_option[$k_2]['ali_value']}&gt;{$goods_option[$k_1]['ali_value']}"]['specId'],
                                        'color'  => $color_val,
                                        'size'   => $size_val,
                                        //'img' => $form_data['images'][$aliyun_sku['color_img'][$k_2]]['file_name']
                                    );
                                }
                            }
                        }

                        if ($goods_supp['cn_name'] == '定制产品'){
                            foreach ($sku as $key => $value) {
                                $sku[$key]['specid'] = '定制产品';
                            }
                        }

                        foreach ($sku as $k => $item) {
                            //新版本sku生成规则
                            /** @var good $good_lib */
                            $good_lib   = get_lib('good');
                            $sku_num_sn = $good_lib->create_sku($prepare, $item,$form_data['type_id']);

                            if ( !$sku_num_sn){
                                $this->sys_message->wrong('sku生成异常!');
                            }

                            //                        $sku_num_sn = $prepare['goods_sn'] . $colors['color'][$item['color']]['option_id'] . $colors['color'][$item['color']]['color_on'] . $size['size'][$item['size']]['size_on'];

                            $r = array_filter($sku, function($t) use ($sku_num_sn) {
                                return $t['sku_num_sn'] == $sku_num_sn;
                            });
                            if ($r || !$item['color'] || !$item['size']){
                                unset($sku[$k]);
                                continue;
                            }

                            $sku[$k]['sku_num_sn'] = $sku_num_sn;
                            $sku[$k]['handle']     = $prepare_sku[$sku_num_sn] ? 'update' : 'insert';
                            $sku[$k]['sku_sn']     = $sku_num_sn;
                            $sku[$k]['sku_id']     = intval($prepare_sku[$sku_num_sn]['sku_id']);
                        }

                        !$sku && $this->sys_message->wrong('请上传SKU!');
                        if ($prepare_sku){
                            foreach ($prepare_sku as $row) {
                                $sku_num_sn = $row['sku_num_sn'];
                                $r          = array_filter($sku, function($t) use ($sku_num_sn) {
                                    return $t['sku_num_sn'] == $sku_num_sn;
                                });
                                if ( !$r){
                                    $sku[] = array(
                                        'handle' => 'delete',
                                        'sku_id' => $row['sku_id'],
                                    );
                                }
                            }
                        }
                        $form_data['sku'] = $sku;

                        $this->prepare_goods_model->prepare_update($form_data, 4, $prepare_id);
                        $this->sys_message->success('成功!');
                    }
                    $tags = $this->db->from('tags')->where(['is_show' => 1])->get()->result_array();
                    break;
            }

            $size_option  = $size['option'];
            $color_option = $colors['option'];
            $fabric       = $this->db->from('fabric')->where(['is_delete' => 0])->get()->result_array();
            foreach ($fabric as $key => $value) {
                if ($value['pid'] == 0){
                    $arr1[] = $value;
                }
            }
            foreach ($arr1 as $key => $value) {
                foreach ($fabric as $k => $v) {
                    if ($value['id'] == $v['pid']){
                        $arr1[$key][$v['pid']][] = $v;
                    }
                }
            }
            $purchase = $this->db->from('purchase')->where(['is_delete' => 0])->get()->result_array();
            $goods_typestyle = $this->db->from('goods_typestyle')->where(['pid'=>0,'is_del'=>0])->get()->result_array();
            foreach ($goods_typestyle as $key => $value) {
                $goods_type = $this->db->from('goods_typestyle')->where(['pid'=>$value['id'],'is_del'=>0])->get()->result_array();
                $goods_typestyle[$key]['val'] = $goods_type;
            }

            $data     = array(
                'goods_typestyle'  => $goods_typestyle,
                'purchase'         => $purchase,
                'arr1'             => json_encode($arr1),
                'tags'             => $tags,
                'step'             => $step,
                'admin_name'       => $admin_name,
                'goods_sn'         => $this->prepare_goods_model->goods_sn_max($_SESSION['admin_id']),
                'cate_option'      => $cate_option,
                'suppliers_option' => $suppliers_option,
                'size_option'      => $size_option,
                'color_option'     => $color_option,
                'type_option'      => $type_option,
                'prepare'          => $prepare,
                'goods_supp'       => $goods_supp,
                'goods_option'     => $_goods_option,
                'goods_album'      => $goods_album,
                'sku'              => $sku,
                'channel'          => $this->config->item('channel'),

            );

            $this->load->view("prepare_set_{$step}", $data);
        }

        /**
         *
         * 获取货号
         *
         */
        public function get_goods_sn()
        {

            $this->load->library('session');

            $this->load->model('prepare_goods_model');

            $i_admin_id = intval($this->input->post('admin_id'));

            $admin_id = intval($i_admin_id ? $i_admin_id : $_SESSION['admin_id']);

            $goods_sn = $this->prepare_goods_model->goods_sn_max($admin_id);

            $this->session->set_userdata('admin_id', $admin_id);


            echo $goods_sn;
            exit;
        }


        public function getProductColor()
        {
            $this->load->library('Eccang');
            $res   = $this->eccang->getProductColor();
            $color = $this->db->from('colors')->get()->result_array();

            foreach ($res['data'] as $key => $value) {
                foreach ($color as $k => $v) {

                    if (strtolower($value['pColorNameEn']) == strtolower($v['color_en'])){
                        $this->db->where(['color_en' => $value['pColorNameEn']]);
                        $this->db->update('colors', ['e_id' => $value['pColorId']]);
                        continue;
                    }
                }
            }
        }

        public function getProductSize()
        {
            $this->load->library('Eccang');
            $res = $this->eccang->getProductSize();

            $size = $this->db->from('size')->get()->result_array();
            foreach ($res['data'] as $key => $value) {
                foreach ($size as $k => $v) {

                    if ($value['pSizeName'] == $v['size_value']){
                        $this->db->where(['size_value' => $value['pSizeName']]);
                        $this->db->update('size', ['e_id' => $value['pSizeId']]);
                        continue;
                    }
                }
            }
        }

        public function getSupplierLista()
        {
            $page = $this->input->get('page');
            $this->load->library('Eccang');
            $arr       = ['pageSize' => 100, 'page' => intval($page)];
            $res       = $this->eccang->getSupplierLista($arr);
            $suppliers = $this->db->from('suppliers')->get()->result_array();
            foreach ($res['data'] as $key => $value) {
                foreach ($suppliers as $k => $v) {
                    if ($value['supplierName'] == $v['suppliers_name']){
                        $this->db->where(['suppliers_name' => $value['supplierName']]);
                        $this->db->update('suppliers', ['e_code' => $value['supplierCode']]);
                        continue;
                    }
                }
            }
        }

        /**
         *
         * 获取1688商品
         *
         */
        public function ajax_ali_goods()
        {
            $this->load->model('suppliers_model');

            $this->load->library('ali');
            $this->load->library('sys_message');
            $url = trim($this->input->post('ali_url'));
            $in_price = $this->input->post('in_price');
            !$url && $this->sys_message->wrong('请填写URL!');

            $supp_goods = $this->suppliers_model->goods_supp_info(array('supp_url' => $url, 'prepare_id' => ''));

            if ($supp_goods){
                // if($supp_goods['goods_id'] || $supp_goods['prepare_id']){
                //     $this->sys_message->wrong('1688链接已经填写过啦!');
                // }
                $_ali_goods = json_decode($supp_goods['goods_json'], true);

                $skuProps = $_ali_goods['skuProps'];
            } else {
                $info       = $this->ali->pu_goods($url);
                $_ali_goods = $info['data'];
                if($_ali_goods == '商品信息获取失败'){
                    $this->sys_message->wrong('商品信息获取失败');
                }
                if (is_array($_ali_goods)){
                    //$ali_price = explode('-', $_ali_goods['displayPrice']);
                    $skuProps  = $_ali_goods['skuProps'];

                    $attributes = array();
                    foreach ($_ali_goods['attributes'] as $row) {
                        $row                 = explode(':', $row);
                        $attributes[$row[0]] = $row[1];
                    }
                    if($in_price > 20){
                        $shop_price = round($in_price / 0.14 / 7,2);
                    }else{
                        $shop_price = round($in_price / 0.14 / 7 + 10,2);
                    }
                    $supp_goods = array(
                        'shop_price'   => $shop_price,
                        'cn_name'    => $_ali_goods['title'],
                        'supp_url'   => $url,
                        'goods_json' => json_encode($_ali_goods),
                        'supp_sn'    => $attributes['货号'],
                    );

                    $supp_goods['g_supp_id'] = $this->suppliers_model->insert_goods_supp($supp_goods);
                } else {
                    $this->sys_message->wrong('获取失败!');
                }

            }

            $re_ali_goods = array(
                'ali_price'  => $in_price,
                'shop_price' => $supp_goods['shop_price'],
                'ali_sn'    => $supp_goods['supp_sn'],
                'skuProps'  => $skuProps,
                'title'     => $supp_goods['cn_name'],
                'g_supp_id' => $supp_goods['g_supp_id'],
            );

            $this->sys_message->success('成功!', $re_ali_goods);
        }
    }
